package cn.gwm.flink.streaming.strategy.vehiclepredict;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.state.MapState;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author : GW00280745
 * @date : 2023/5/4 8:17
 * @description :
 * 实时更新每1%soc的时间到state中 保留最近的一次时间
 */
public class OneSocState implements Serializable {
    public OneSocState(){
    }
    public void process(JSONObject line,MapState<String, String> oneSocTimeState){
        try {
            String vinVal = line.getString(ChargePredictFields.ins().srcFieldVin);
            Long tidVal = line.getLong(ChargePredictFields.ins().srcFieldTid);
            String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
            Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
            String chargeId = vinVal.concat(startChargeTime);
            //先判断状态是否有数据
            boolean contains = oneSocTimeState.contains(chargeId);
            //没有，则state中的值是0
            String socState = null;
            if (!contains){
                socState = tidVal.toString().concat(ChargePredictFields.ins().char3).concat(bmsSoc.toString());
                oneSocTimeState.put(chargeId,socState);
            }else {
                //有，则进行判断 soc差值是否为整数
                socState = oneSocTimeState.get(chargeId);
            }
            float delta = deltaSoc(bmsSoc, socState);
            //判断 soc差值是否为整数 找大于0.9的第一个值
            boolean b = delta >= 1.0 ;
            if (!b){
                return;
            }
            //符合条件则计算时间差
            String[] split = socState.split(ChargePredictFields.ins().char3);
            String timeState = split[0];
            long l = Long.parseLong(timeState);
            long deltaTime = tidVal - l;
            long timeDelta = deltaTime / 1000;
            //如果时间差小于60s 那么跳过 跳过了会导致soc差出现2 3 这种值
            long lim = 60;
            if(timeDelta<lim){
                return;
            }
            //平均每个soc需要多少时间
            float v = new BigDecimal(timeDelta).divide(new BigDecimal(delta * 60), 0, BigDecimal.ROUND_HALF_DOWN).floatValue();
            //如果 每个soc需要的时间是0 属于无效值
            int limit0 = 1;
            if (v< limit0){
                return;
            }
            String concat = tidVal.toString().concat(ChargePredictFields.ins().char3)
                    .concat(bmsSoc.toString()).concat(ChargePredictFields.ins().char3)
                    .concat(String.valueOf(v));
            String socValState = split[1];
            String concat1 = tidVal.toString().concat(kangStr).concat(timeState).concat(ChargePredictFields.ins().char3)
                    .concat(socValState).concat(kangStr)
                    .concat(bmsSoc.toString()).concat(ChargePredictFields.ins().char3)
                    .concat(String.valueOf(v));
            //更新到state中
            oneSocTimeState.put(chargeId,concat);
            System.out.println(String.format("vin=%s , startchargetime = %s , state_val=%s ",vinVal,startChargeTime,concat1));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private String kangStr = "-";
    private float deltaSoc(Float bmsSoc, String socState) {
        String[] split = socState.split(ChargePredictFields.ins().char3);
        String s = split[1];
        float v = Float.parseFloat(s);
        float delta = bmsSoc - v;
        return delta;
    }
}
